/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.PackageType;

/**
 *
 * @author Administrator
 */
public class PackageTypeDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;

    public boolean newPackageType(PackageType packType) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_NewPackageType(?,?)}");
            cs.setString("name", packType.getPackageTypeName());
            cs.setString("description", packType.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(PackageTypeDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean updatePackageType(PackageType packType) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_UpdatePackageType(?,?,?)}");
            cs.setInt("packageTypeID",packType.getPackageTypeID());
            cs.setString("name", packType.getPackageTypeName());
            cs.setString("description", packType.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(PackageTypeDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public List<PackageType> listAllPackType(){
        try {
            List<PackageType> list = new ArrayList<PackageType>();
            PackageType pType ;
            cs = conn.createConnection().prepareCall("{call sp_ListAllPackageType}");
            ResultSet rs = cs.executeQuery();

            while(rs.next()){
                pType = new PackageType(rs.getInt("PackageTypeID"),rs.getString("PackageName"),rs.getString("Description"));
                list.add(pType);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(PackageTypeDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
}
